package com.example.wc.logindemolistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by hui on 2017/2/14.
 */

public class BlogsBaseAdapter extends BaseAdapter {

    private List<Blogs> mList;
    private LayoutInflater mInflater;

    public BlogsBaseAdapter(Context context, List<Blogs> list){
        mList = list;
        mInflater = LayoutInflater.from(context);
    }

    //刷新数据
    public void refresh(List<Blogs> list){
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.blogslist_item, null);

            viewHolder.name = (TextView)convertView.findViewById(R.id.blogs_name);
            viewHolder.content = (TextView)convertView.findViewById(R.id.blogs_content);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        Blogs blogs = mList.get(position);
        viewHolder.name.setText(blogs.getName());
        viewHolder.content.setText(blogs.getContent());

        return convertView;
    }

    class ViewHolder {
        public TextView name;
        public TextView content;
    }
}
